all:
	make conf mconf qconf gconf

conf: conf.o zconf.tab.o 
	$(CC) -o $@ conf.o zconf.tab.o

gconf: gconf.o kconfig_load.o zconf.tab.o
	$(CC) -o $@ gconf.o `pkg-config --libs gtk+-2.0 gmodule-2.0 libglade-2.0` kconfig_load.o zconf.tab.o

mconf: mconf.o zconf.tab.o lxdialog/textbox.o lxdialog/yesno.o lxdialog/menubox.o lxdialog/inputbox.o lxdialog/util.o lxdialog/checklist.o lxdialog/dialog.h
	$(CC) -o $@ -lncursesw mconf.o lxdialog/textbox.o lxdialog/yesno.o lxdialog/menubox.o lxdialog/inputbox.o lxdialog/util.o lxdialog/checklist.o lxdialog/dialog.h zconf.tab.o

qconf: qconf.o kconfig_load.o zconf.tab.o
	$(CC) -o $@ qconf.o $(KC_QT_LIBS) -ldl kconfig_load.o zconf.tab.o

lxdialog/textbox.o: lxdialog/textbox.c
	$(CC) -o $@ -c $<

lxdialog/yesno.o: lxdialog/yesno.c
	$(CC) -o $@ -c $<

lxdialog/menubox.o: lxdialog/menubox.c
	$(CC) -o $@ -c $<

lxdialog/inputbox.o: lxdialog/inputbox.c
	$(CC) -o $@ -c $<

lxdialog/util.o: lxdialog/util.c
	$(CC) -o $@ -c $<

lxdialog/checklist.o: lxdialog/checklist.c
	$(CC) -o $@ -c $<

util.o: util.c
	$(CC) -o $@ -c $<

expr.o: expr.c
	$(CC) -o $@ -c $<

menu.o: menu.c 
	$(CC) -o $@ -c menu.c

confdata.o: confdata.c
	$(CC) -o $@ -c $<

symbol.o: symbol.c
	$(CC) -o $@ -c $<

kconfig_load.o: kconfig_load.c
	$(CC) -o $@ -c $<

conf.o: conf.c
	$(CC) -o $@ -c $<

gconf.o: gconf.c .tmp_gtkcheck lkc_defs.h 
	$(CC) -o $@  `pkg-config --cflags gtk+-2.0 gmodule-2.0 libglade-2.0` -D LKC_DIRECT_LINK  -c $<

mconf.o: mconf.c
	$(CC) -o $@ -c $<

qconf.o: qconf.cc .tmp_qtcheck qconf.moc lkc_defs.h
	$(CXX) -o $@ $(KC_QT_CFLAGS) -D LKC_DIRECT_LINK -c $<

lkc_defs.h: lkc_proto.h
	sed < $< > $@ 's/P(\([^,]*\),.*/#define \1 (\*\1_p)/'

zconf.tab.o: lex.zconf.c zconf.hash.c

zconf.tab.c: zconf.y
lex.zconf.c: zconf.l
zconf.hash.c: zconf.gperf

%.tab.c: %.y
	bison -l -b $* -p $(notdir $*) $<
	cp $@ $@_shipped

lex.%.c: %.l
	flex -L -P$(notdir $*) -o$@ $<
	cp $@ $@_shipped

%.hash.c: %.gperf
	gperf < $< > $@
	cp $@ $@_shipped

#### Gtk
.tmp_gtkcheck: Makefile
-include .tmp_gtkcheck

.tmp_gtkcheck:
	@if `pkg-config --exists gtk+-2.0 gmodule-2.0 libglade-2.0`; then               \
		if `pkg-config --atleast-version=2.0.0 gtk+-2.0`; then                  \
			touch $@;                                                               \
		else                                                                    \
			echo "*";                                                       \
			echo "* GTK+ is present but version >= 2.0.0 is required.";     \
			echo "*";                                                       \
			false;                                                          \
		fi                                                                      \
	else                                                                            \
		echo "*";                                                               \
		echo "* Unable to find the GTK+ installation. Please make sure that";   \
		echo "* the GTK+ 2.0 development package is correctly installed...";    \
		echo "* You need gtk+-2.0, glib-2.0 and libglade-2.0.";                 \
		echo "*";                                                               \
		false;                                                                  \
	fi


#### QT
.tmp_qtcheck: Makefile
-include .tmp_qtcheck

.tmp_qtcheck:
	@set -e; echo "  CHECK   qt"; dir=""; pkg=""; \
	pkg-config --exists qt 2> /dev/null && pkg=qt; \
	pkg-config --exists qt-mt 2> /dev/null && pkg=qt-mt; \
	if [ -n "$$pkg" ]; then \
		cflags="\$$(shell pkg-config $$pkg --cflags)"; \
		libs="\$$(shell pkg-config $$pkg --libs)"; \
		moc="\$$(shell pkg-config $$pkg --variable=prefix)/bin/moc"; \
		dir="$$(pkg-config $$pkg --variable=prefix)"; \
	else \
		for d in $$QTDIR /usr/share/qt* /usr/lib/qt*; do \
			if [ -f $$d/include/qconfig.h ]; then dir=$$d; break; fi; \
	 	done; \
		if [ -z "$$dir" ]; then \
			echo "*"; \
			echo "* Unable to find the QT3 installation. Please make sure that"; \
			echo "* the QT3 development package is correctly installed and"; \
			echo "* either install pkg-config or set the QTDIR environment"; \
			echo "* variable to the correct location."; \
			echo "*"; \
			false; \
		fi; \
		libpath=$$dir/lib; lib=qt; osdir=""; \
		$(CXX) -print-multi-os-directory > /dev/null 2>&1 && \
		osdir=x$$($(CXX) -print-multi-os-directory); \
		test -d $$libpath/$$osdir && libpath=$$libpath/$$osdir; \
		test -f $$libpath/libqt-mt.so && lib=qt-mt; \
		cflags="-I$$dir/include"; \
		libs="-L$$libpath -Wl,-rpath,$$libpath -l$$lib"; \
		moc="$$dir/bin/moc"; \
	fi; \
	if [ ! -x $$dir/bin/moc -a -x /usr/bin/moc ]; then \
		echo "*"; \
		echo "* Unable to find $$dir/bin/moc, using /usr/bin/moc instead."; \
		echo "*"; \
		moc="/usr/bin/moc"; \
	fi; \
	echo "KC_QT_CFLAGS=$$cflags" > $@; \
	echo "KC_QT_LIBS=$$libs" >> $@; \
	echo "KC_QT_MOC=$$moc" >> $@

%.moc: %.h
	$(KC_QT_MOC) -i $< -o $@

#### Cleaning
clean:
	ls *.o */*.o | xargs rm -f
	rm -f lex.*.c*
	rm -f *.tab.c*
	rm -f *.moc
	rm -f lkc_defs.h
	rm -f conf mconf gconf qconf
	rm -f .tmp_gtkcheck .tmp_qtcheck

